﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.Collections.ObjectModel;
using MageWars.Support;
using Microsoft.Xna.Framework.Graphics;

namespace MageWars.GameElements
{
    public class Projectile : Movable
    {
        static Projectile()
        {
            _projectiles = new List<Projectile>();
        }

        public Projectile(Mage Caster, Team Group)
        {
            this.Group = Group;
            this.Caster = Caster;
            RemainingPower = 30;
            Speed = 5;
            _projectiles.Add(this);
        }

        public Mage Caster { get; set; }


        private static List<Projectile> _projectiles;

        public int RemainingPower { get; private set; }
        public float Speed { get; private set; }

        private bool destroyed;

        public void UpdatePosition()
        {
            if (!destroyed && RemainingPower > 1)
            {
                RemainingPower -= 1;
                float x = Position.X + (float)Math.Cos(Direction) * Speed;
                float y = Position.Y + (float)Math.Sin(Direction) * Speed;
                var nv = new Vector3(x, y, 0);

                bool collided = !MageMath.IsTargetTransitable(new Point((int)nv.X, (int)nv.Y));
                //if (collided)
                //{
                //   // Destroy();
                //}
                //else
                //{
                Position = nv;
                //}
            }
            else
            {
                Destroy();
            }
        }

        public void Destroy()
        {
            if (!destroyed)
            {
                destroyed = true;
                Position = Vector3.Zero;
                _projectiles.Remove(this);
            }
        }

        public override Texture2D CurrentTexture()
        {
            if (this.Group == Team.TeamBlue)
            {
                return Textures.Team1Spell;
            }
            else
            {
                return Textures.Team2Spell;
            }
        }

        public static IEnumerable<Projectile> Projectiles
        {
            get
            {
                return _projectiles.ToArray();
            }
        }
    }
}
